package com.zsyh.jiujiu.home;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ActivityUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.zyh.toolslib.base.BaseActivity;
import com.example.zyh.toolslib.utils.SPUtils;
import com.example.zyh.toolslib.widget.TitleBar;
import com.google.gson.Gson;
import com.lwb.retrofit.RetrofitTask;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.bean.MyAdressBean;
import com.zsyh.jiujiu.net.MyObserver;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @ProjectName: livvehelp
 * @Package: com.zsyh.lsb.ui
 * @ClassName:
 * @Description: java类作用描述
 * @Author: zsyh
 * @CreateDate: 2020/9/7 11:59
 * @UpdateUser: liangwanbo
 * @UpdateDate: 2020/9/7 11:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class AdressManger extends BaseActivity {

    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.adresslist)
    RecyclerView adresslist;
    @BindView(R.id.add)
    RelativeLayout add;

    private BaseQuickAdapter<MyAdressBean.DataBean, BaseViewHolder> mAdapter;
    List<MyAdressBean.DataBean> list = new ArrayList<>();

    @Override
    protected int initLayout() {
        return R.layout.activity_adress;
    }

    @Override
    protected void initView() {
        titleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {

        mAdapter=new BaseQuickAdapter<MyAdressBean.DataBean, BaseViewHolder>(R.layout.item_address,list) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, MyAdressBean.DataBean item) {
                helper.setText(R.id.name,item.getUserName());
                helper.setText(R.id.phone,item.getUserPhone());
                helper.setText(R.id.adreess,item.getAddressProvname()+item.getAddressCityname()+item.getAddressDistname()+item.getAddressDesc());
                if(item.getIsDefault().equals("1")){
                    helper.setVisible(R.id.moren,true);
                }else{
                    helper.setVisible(R.id.moren,false);
                }

                helper.setOnClickListener(R.id.bianji, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bundle bundlexian = new Bundle();
                        bundlexian.putString("pname", item.getAddressProvname());
                        bundlexian.putString("cname", item.getAddressCityname());
                        bundlexian.putString("dname", item.getAddressDistname());
                        bundlexian.putString("provinceCodes", item.getAddressProv());
                        bundlexian.putString("cityCodes", item.getAddressCity());
                        bundlexian.putString("areaCodes", item.getAddressDist());
                        bundlexian.putString("ismoren", item.getIsDefault());
                        bundlexian.putString("shoujihao", item.getUserPhone());
                        bundlexian.putString("xingming", item.getUserName());
                        bundlexian.putString("dizhi", item.getAddressDesc());
                        bundlexian.putString("dizhiid", item.getId());
                        ActivityUtils.startActivity(bundlexian, AddAdress.class);
                    }
                });

                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent();
                        intent.putExtra("phone", item.getUserPhone());
                        intent.putExtra("name",item.getUserName());
                        intent.putExtra("adress", item.getAddressProvname()+item.getAddressCityname()+item.getAddressDistname()+item.getAddressDesc());
                        intent.putExtra("id", item.getId());
                        intent.putExtra("ismoren", item.getIsDefault());
                        setResult(2, intent);
                        thisActivity.finish();
                    }
                });
            }
        };
        adresslist.setLayoutManager(new LinearLayoutManager(thisActivity));
        adresslist.setAdapter(mAdapter);


    }

    @Override
    protected void onResume() {
        super.onResume();
        myadress((String) SPUtils.get(thisActivity, "userid", ""));
    }

    @Override
    protected void initEvent() {

    }



    @OnClick(R.id.add)
    public void onViewClicked() {
        Bundle bundlexian = new Bundle();
        bundlexian.putString("pname", "");
        bundlexian.putString("cname", "");
        bundlexian.putString("dname","");
        bundlexian.putString("provinceCodes", "");
        bundlexian.putString("cityCodes", "");
        bundlexian.putString("areaCodes", "");
        bundlexian.putString("ismoren", "");
        bundlexian.putString("shoujihao", "");
        bundlexian.putString("xingming", "");
        bundlexian.putString("dizhi", "");
        bundlexian.putString("dizhiid", "");
        ActivityUtils.startActivity(bundlexian, AddAdress.class);
    }

    /**
     * 我的地址
     */
    public void myadress(String userid){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .selectAddress(userid)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(this,false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("mydizhi",JsonStr+"");
                        Gson gson=new Gson();
                        MyAdressBean myAdressBean=gson.fromJson(JsonStr,MyAdressBean.class);
                        if(list!=null){
                            list.clear();
                        }
                        for(int i=0;i<myAdressBean.getData().size();i++){
                            list.add(myAdressBean.getData().get(i));
                        }
                        mAdapter.notifyDataSetChanged();

                    }
                });

    }

}
